package flc.ast.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.stark.jigsaw.puzzle.PuzzleLayout;
import com.stark.jigsaw.puzzle.template.slant.NumberSlantLayout;
import com.stark.jigsaw.puzzle.template.straight.NumberStraightLayout;

import java.util.List;

import flc.ast.utils.PuzzleLayoutItemViewHolder;

public class PuzzleLayoutAdapter extends RecyclerView.Adapter<PuzzleLayoutItemViewHolder> implements View.OnClickListener {

    private List<PuzzleLayout> listDatas;
    private PuzzleLayoutItemViewHolder mSelViewHolder;
    private IOnItemClickListener listener;
    private int mSelectIdx = 0;
    private @LayoutRes int mLayoutId;

    public PuzzleLayoutAdapter(@LayoutRes int layoutId) {
        mLayoutId = layoutId;
    }

    @Override
    public PuzzleLayoutItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(mLayoutId, viewGroup, false);
        PuzzleLayoutItemViewHolder holder = new PuzzleLayoutItemViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(PuzzleLayoutItemViewHolder puzzleLayoutItemViewHolder, int i) {
        PuzzleLayout layout = listDatas.get(i);
        puzzleLayoutItemViewHolder.bindData(layout, i);
        View itemView = puzzleLayoutItemViewHolder.itemView;
        itemView.setTag(puzzleLayoutItemViewHolder);
        if (i == mSelectIdx) {
            mSelViewHolder = puzzleLayoutItemViewHolder;
            puzzleLayoutItemViewHolder.showSelectView(true);
        } else {
            puzzleLayoutItemViewHolder.showSelectView(false);
        }
    }

    @Override
    public int getItemCount() {
        return listDatas == null ? 0 : listDatas.size();
    }

    public void setListDatas(List<PuzzleLayout> listDatas) {
        this.listDatas = listDatas;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        PuzzleLayoutItemViewHolder holder = (PuzzleLayoutItemViewHolder) v.getTag();
        if (holder == mSelViewHolder) {
            return;
        }
        if (mSelViewHolder != null) {
            mSelViewHolder.showSelectView(false);
        }
        mSelViewHolder = holder;
        holder.showSelectView(true);
        mSelectIdx = holder.getBindPos();
        if (listener != null) {
            listener.onItemClick(holder.getPuzzleLayout());
        }
    }

    public int getPuzzleLayoutPos(@NonNull PuzzleLayout layout) {
        int pos = 0;
        if (listDatas == null || listDatas.size() == 0) {
            return pos;
        }

        PuzzleLayout tmpLayout;
        for (int i = 0; i < listDatas.size(); i++) {
            tmpLayout = listDatas.get(i);
            if (layout.getClass() != tmpLayout.getClass()) {
                continue;
            }
            if (layout instanceof NumberStraightLayout) {
                NumberStraightLayout straightLayout = (NumberStraightLayout) layout;
                NumberStraightLayout tmpStraightLayout = (NumberStraightLayout) tmpLayout;
                if (straightLayout.getTheme() == tmpStraightLayout.getTheme()) {
                    pos = i;
                    break;
                }
            } else if (layout instanceof NumberSlantLayout) {
                NumberSlantLayout slantLayout = (NumberSlantLayout) layout;
                NumberSlantLayout tmpSlantLayout = (NumberSlantLayout) tmpLayout;
                if (slantLayout.getTheme() == tmpSlantLayout.getTheme()) {
                    pos = i;
                    break;
                }
            }
        }
        return pos;
    }

    public void setSelectIdx(int selectIdx) {
        if (mSelectIdx != selectIdx) {
            mSelectIdx = selectIdx;
            notifyDataSetChanged();
        }
    }

    public void setListener(IOnItemClickListener listener) {
        this.listener = listener;
    }

    public interface IOnItemClickListener {

        void onItemClick(PuzzleLayout puzzleLayout);

    }
}
